package com.xt.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.xt.website.dao.CategoryDao;
import com.xt.website.dao.NewsDao;
import com.xt.website.po.CategoryExt;
import com.xt.website.po.CategorySecond;
import com.xt.website.po.News;
import com.xt.website.po.NewsExt;
import com.xt.website.service.NewsService;
import com.xt.website.util.MyPageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  NewsServiceImpl
 * @Date 2019/8/18/13:38
 * @Description TODO:(一句话描述这个类)
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsDao newsDao;
    @Resource
    private CategoryDao categoryDao;


    /**
     * 根据一级类目id查询全部新闻
     */
    @Override
    public MyPageInfo<News> findNewsByCid(Integer pageNow, Integer cid,Integer pageSize) {
        if(pageSize==null){
           pageSize=3;
        }
        PageHelper.startPage(pageNow,pageSize);
        //查询新闻结果集
        List<News> allNews = newsDao.findAllNewsByCid(cid);

        //返回新闻结果集，allNews：新闻结果集   navigatePages =5  设置页码数为5
        MyPageInfo<News> pageInfo = new MyPageInfo<News>(allNews,5);

        //查询当前类目CategoryExt信息
        CategoryExt<CategorySecond> categoryExt = categoryDao.findCategoryByCid(cid);
        pageInfo.setCategoryExt(categoryExt);

        return pageInfo;
    }


    /**
     * 根据二级类目查询全部新闻
     * @param pageNow
     * @param csid
     * @return
     */
    @Override
    public MyPageInfo<News> findNewsByCsid(Integer pageNow, Integer csid,Integer pageSize) {
        if(pageSize==null){
            pageSize=3;
        }
        PageHelper.startPage(pageNow,pageSize);
        //查询新闻结果集
        List<News> allNews = newsDao.findAllNewsByCsid(csid);

        //返回新闻结果集，allNews：新闻结果集   navigatePages =5  设置页码数为5
        MyPageInfo<News> pageInfo = new MyPageInfo<News>(allNews,5);
        
        //查询当前类目CategorySecond信息
        CategorySecond categorySecond = categoryDao.findCategorySecondByCsid(csid);
        pageInfo.setCategorySecond(categorySecond);

        return pageInfo;
    }


    /**
     * 根据新闻id获取新闻详情
     * @param id   新闻id
     * @return
     */
    @Override
    public News findNewsByID(Integer id) {
        return newsDao.findNewsByID(id);
    }


    /**
     * 根据新闻id获取NewsExt
     * @param news
     * @return
     */
    @Override
    public NewsExt findNewsExt(News news) {
        NewsExt newsExt = newsDao.findNewsByID(news.getId());
        News prev = null;
        News next = null;
        if(news.getCsid()==0|| news.getCsid()==null){
            //一级类目下的上一条下一条
            prev = newsDao.finPrevNewsByID(news.getId());
            next = newsDao.finNextNewsByID(news.getId());
        }else{
            //二级类目下的上一条下一条
            prev = newsDao.finPrevNewsByIDAndCsid(news);
            next = newsDao.finNextNewsByIDAndCsid(news);
        }
        newsExt.setNext(next);
        newsExt.setPrev(prev);

        return newsExt;
    }
}
